package com.xiaode.plugin.obs;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import com.obs.services.ObsClient;

import java.io.IOException;

public class ObsPlugin implements IPlugin{
	
	private String endPoint;
	public static String ak;
	public static String xiaodeBucketName;
	public static String chatBucketName;
	private String sk;
	public static long expireSeconds;
	public static String chatUrl;
	public static String xiaodeUrl;
	public static ObsClient obsClient;
	
	public ObsPlugin(String configFile){
		Prop p = PropKit.use(configFile);
		ak = p.get("obs.accessKey");
		this.sk = p.get("obs.secretKey");
		expireSeconds = p.getLong("obs.expireSeconds");
		this.endPoint = p.get("obs.endPoint");
		chatBucketName = p.get("obs.bucketName.chat");
		xiaodeBucketName = p.get("obs.bucketName.xiaode");
		chatUrl = p.get("obs.uploadUrl.chat");
		xiaodeUrl = p.get("obs.uploadUrl.xiaode");
	}
	
	public ObsPlugin(String accessKey, String secretKey, long es, String ep, String bn){
		ak = accessKey;
		this.sk = secretKey;
		expireSeconds = es;
		this.endPoint = ep;
	}
	
	@Override
	public boolean start() {
		if(obsClient == null) {
			obsClient = new ObsClient(ak, sk, endPoint);
		}
		return true;
	}

	@Override
	public boolean stop() {
		try {
			obsClient.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	
}
